<?php

require_once('../application/modules/framework/models/DbTable/Frameworks.php');
require_once('../application/modules/framework/forms/AddFramework.php');

class Framework_FrameworkController extends Zend_Controller_Action {

    public function init() {
        /* Initialize action controller here */
    }

    public function indexAction() {
        // action body
    }

    public function listAction() {
        $aFramework = new Framework_Model_DbTable_Frameworks;
        //var_dump($aFramework);
        $this->view->aFrameworks = $aFramework->fetchAll()->toArray();
    }

    public function addAction() {
        $aFramework = new Framework_Model_DbTable_Frameworks;
        $form = new Framework_Form_AddFramework();
        if ($this->getRequest()->isPost()) {
            $data = $this->_request->getParams();
            if ($form->isValid($data)) {
                $aFramework->createRow();
                $lastInsertId = $aFramework->addFramework($data);
                if ($lastInsertId) {
                    $this->_helper->redirector('detail', 'framework', 'framework', array('id' => $lastInsertId));
                }
            } else {
                $form->populate($data);
            }
        }
        $this->view->form = $form;
    }

    public function detailAction() {
        $aFramework = new Framework_Model_DbTable_Frameworks();
        $id = $this->_request->getParam('id');
        $row = $aFramework->find($id)->toArray();
        $this->view->data = current($row);
    }
    
    public function deleteAction() {
        $aFramework = new Framework_Model_DbTable_Frameworks();
        $id = $this->_request->getParam('id');
        $row = $aFramework->find($id)->toArray();
        $this->view->data = current($row);
        if ($this->getRequest()->isPost()) {
            if($this->_request->getPost('submit')){
                $aFramework->deleteFramework($id);
                $this->_helper->redirector('list', 'framework', 'framework');
            }elseif($this->_request->getPost('cancel')){
                $this->_helper->redirector('list', 'framework', 'framework');
            }
        }
    }

    public function updateAction() {
        $aFramework = new Framework_Model_DbTable_Frameworks;
        $form = new Framework_Form_AddFramework();
        $id = $this->_request->getParam('id');

        if ($this->getRequest()->isPost()) {
            $data = $this->_request->getPost();
            if ($form->isValid($data)) {
                $updated = $aFramework->updateFramework($id, $data);
            } else {
                $form->populate($data);
            }
        }
        $row = $aFramework->find($id)->toArray();
        $form->populate(current($row));
        $this->view->form = $form;
    }

}

